
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
        <div class="layui-card">
            <div class="layui-card-body">
            <div class="layui-btn-group" style="margin-bottom: 10px;width: 100%;">
                <button class="layui-btn layuiadmin-btn-tags" style="float: right;" data-type="add">添加名家</button> 	
            </div>
            
            <table class="layui-hide" id="test-table-operate" lay-filter="test-table-operate"></table>
            <script type="text/html" id="LAY-good-list-table-img">
	            <img src="{{d.author_image}}" alt="" style="width:82px; height: 110px;">
	        </script>
	        <script type="text/html" id="LAY-good-list-table-stock">
	            <p>{{d.book_num}}本</p>
	        </script>
            <script type="text/html" id="test-table-operate-barDemo">
                <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
            </script>
            </div>
        </div>
        </div>
    </div>
    </div>
<script>
layui.use(['admin', 'table', 'form', 'upload'], function(){
    var table = layui.table
    ,form = layui.form
    ,admin = layui.admin
    ,upload = layui.upload
    ,view = layui.view
    ,element = layui.element
    ,$ = layui.$
	,cols = [
		[
			{
				align: 'center',
				width: 110,
				templet: '#LAY-good-list-table-img',
				title: '名家照片',
				field: 'author_image'
			}, {
				align: 'center',
				field: 'author_name',
				title: '姓名'
			}, {
				align: 'center',
				field: 'country',
				title: '国籍',
			}, {
				align: 'center',
				field: 'book_num',
				title: '已录入的作品数量',
				sort: true,
				templet: '#LAY-good-list-table-stock'
			}, {
				align: 'center',
				field: 'sort',
				title: '排序',
			}, {
				align: 'center',
				fixed: 'right',
				title: '操作',
				toolbar: '#test-table-operate-barDemo'
			}
		]
	];
	table.set({
		headers: { //通过 request 头传递
			'access-token': layui.data('layuiAdmin').access_token
		}
	});
	table.render({
		elem: '#test-table-operate',
		url: 'https://bookshop.dingdangtoy.com/api/admin/author/index',
		parseData: function(res) { //res 即为原始返回的数据
			return {
				"code": res.code, //解析接口状态
				"msg": res.message, //解析提示文本
				"data": res.data.items, //解析数据列表
				"count": res.data._meta.total_count //解析数据长度
				
			}
		},
		cols: cols,
		page: true
	});
    
    //监听工具条
    table.on('tool(test-table-operate)', function(obj){
      var data = obj.data;
      if(obj.event === 'edit'){
        admin.popup({
        title: '编辑名家'
        ,area: ['80%', '80%']
        ,id: 'LAY-popup-content-tags'
        ,success: function(layero, index){
          view(this.id).render('columns/all/add', data).done(function(){
            form.render(null, 'layuiadmin-form-tags');
            var content_intro = '';
            var E = window.wangEditor
	        var editor = new E('#editor')
	        editor.customConfig.onchange = function (html) {
	            // 监控变化，同步更新到 textarea
               content_intro = html
	        }
	        editor.create();
	        // 获取名家
			$.ajax({
				type: "GET",
				dataType: "json",
				url: "https://bookshop.dingdangtoy.com/api/admin/author/show?id=" + data.id,
				data: {},
				headers: { //通过 request 头传递
					'access-token': layui.data('layuiAdmin').access_token
				},
				success: function(data) {
                    editor.txt.html(data.data.intro)
                    content_intro = data.data.intro;
					form.render();
				},
				error: function(msg) {
					console.log(msg)
				}
			});
	        
            if (data.id) {
            	$('#test-table-operate1').show();
            }
	    var cols1 = [
		[
			{
				align: 'center',
				width: 110,
				templet: '#LAY-good-list-table-img1',
				title: '封面',
				field: 'image'
			}, {
				align: 'center',
				field: 'title',
				title: '书名'
			}, {
				align: 'center',
				field: 'book_num',
				title: '价格',
				templet: '#LAY-good-list-table-stock1'
			}, {
				align: 'center',
				field: 'total_stock',
				title: '库存状态',
			}
		]
	];
	table.set({
		headers: { //通过 request 头传递
			'access-token': layui.data('layuiAdmin').access_token
		}
	});
	table.render({
		elem: '#test-table-operate1',
		url: "https://bookshop.dingdangtoy.com/api/admin/author/show?id="+ data.id,
		parseData: function(res) { //res 即为原始返回的数据
			return {
				"code": res.code, //解析接口状态
				"msg": res.message, //解析提示文本
					"data": res.data.items, //解析数据列表				
				}
				},
				cols: cols1,
					page: false
				});//上传图片
			  upload.render({
				elem: '#add-goods-upload-btn',
				url: 'https://bookshop.dingdangtoy.com/api/admin/image/store',
				accept: 'images',
				method: 'get',
				acceptMime: 'image/*',
				done: function(res) {
					console.log(res.data.url)
					$('#add-goods-upload-img').attr('src', res.data.url);
					$('#add-goods-upload-image').val(res.data.url)
					//$(this.item).prev("div").children("input").val(res.data.url)
				}
			  }); 
			  upload.render({
				elem: '#add-goods-upload-btn1',
				url: 'https://bookshop.dingdangtoy.com/api/admin/image/store',
				accept: 'images',
				method: 'get',
				acceptMime: 'image/*',
				done: function(res) {
					console.log(res.data.url)
					$('#add-goods-upload-img1').attr('src', res.data.url);
					$('#add-goods-upload-image1').val(res.data.url)
					//$(this.item).prev("div").children("input").val(res.data.url)
				}
			  });
            //监听提交
            form.on('submit(layuiadmin-app-tags-submit)', function(data){	
              var title = data.field.title; //获取提交的字段
              var sort = data.field.sort;
              //提交 Ajax 成功后，关闭当前弹层并重载表格
			        layer.confirm('是否执行此操作', function(index){
			   	        $.ajax({
						    type: "PUT",
						    dataType: "json",
						    url: "https://bookshop.dingdangtoy.com/api/admin/author/update",
						    data: {
						    	id:data.field.id,
						    	'author_name':data.field.author_name,
						    	'country':data.field.country,
						    	'sort': data.field.sort,
						    	'author_image':data.field.author_image,
						    	'intro':content_intro,
						    	'list_image':data.field.list_image
						    },
						    headers: { //通过 request 头传递
				              'access-token': layui.data('layuiAdmin').access_token
				            },
									success: function(data){
									    if (parseInt(data.code) == 0) {
									    	layer.msg('操作成功');
										    layui.table.reload('test-table-operate');
									    } else {
									    	layer.msg(data.message);
									    }
									},
									error: function(msg){
									  console.log(msg)
									}
							});
			        });			         
				layer.close(index); //执行关闭 
            });
          });
        }
      });
      } else if(obj.event === 'del'){
        layer.confirm('真的删除行么', function(index){
      		$.ajax({
				type: "DELETE",
				dataType: "json",
				url: "https://bookshop.dingdangtoy.com/api/admin/author/destroy",
				data: {
					id: data.id
				},
				headers: { //通过 request 头传递
					'access-token': layui.data('layuiAdmin').access_token
				},
				success: function(data) {
					layer.msg('操作成功');
					layui.table.reload('test-table-operate');
					layer.close(index); //执行关闭 
				},
				error: function(msg) {
					console.log(msg)
				}
			});
        });
      }
    });
  var active = {
    add: function(){
      admin.popup({
        title: '添加名家'
        ,area: ['80%', '80%']
        ,id: 'LAY-popup-content-tags'
        ,success: function(layero, index){
          view(this.id).render('columns/all/add').done(function(){
            form.render(null, 'layuiadmin-form-tags');
            var content_intro = '';
            var E = window.wangEditor
	        var editor = new E('#editor')
	        editor.customConfig.onchange = function (html) {
	            // 监控变化，同步更新到 textarea
               content_intro = html
	        }
	        editor.create();
              //上传图片
			  upload.render({
				elem: '#add-goods-upload-btn',
				url: 'https://bookshop.dingdangtoy.com/api/admin/image/store',
				accept: 'images',
				method: 'get',
				acceptMime: 'image/*',
				done: function(res) {
					console.log(res.data.url)
					$('#add-goods-upload-img').attr('src', res.data.url);
					$('#add-goods-upload-image').val(res.data.url)
					//$(this.item).prev("div").children("input").val(res.data.url)
				}
			  }); 
			  upload.render({
				elem: '#add-goods-upload-btn1',
				url: 'https://bookshop.dingdangtoy.com/api/admin/image/store',
				accept: 'images',
				method: 'get',
				acceptMime: 'image/*',
				done: function(res) {
					console.log(res.data.url)
					$('#add-goods-upload-img1').attr('src', res.data.url);
					$('#add-goods-upload-image1').val(res.data.url)
					//$(this.item).prev("div").children("input").val(res.data.url)
				}
			  });
            //监听提交
            form.on('submit(layuiadmin-app-tags-submit)', function(data){	
              var title = data.field.title; //获取提交的字段
              var sort = data.field.sort;
              //提交 Ajax 成功后，关闭当前弹层并重载表格
			        layer.confirm('是否执行此操作', function(index){
			   	        $.ajax({
						    type: "POST",
						    dataType: "json",
						    url: "https://bookshop.dingdangtoy.com/api/admin/author/store",
						    data: {
						    	'author_name':data.field.author_name,
						    	'sort':data.field.sort,
						    	'country':data.field.country,
						    	'author_image':data.field.author_image,
						    	'intro':content_intro,
						    	'list_image':data.field.list_image
						    },
						    headers: { //通过 request 头传递
				              'access-token': layui.data('layuiAdmin').access_token
				            },
									success: function(data){
									    if (parseInt(data.code) == 0) {
									    	layer.msg('操作成功');
										    layui.table.reload('test-table-operate');
									    } else {
									    	layer.msg(data.message);
									    }
									},
									error: function(msg){
									  console.log(msg)
									}
							});
			        });			         
				layer.close(index); //执行关闭 
            });
          });
        }
      });
    }
    }
    
  $('.layui-btn.layuiadmin-btn-tags').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  }); 
  
  });
</script>